package com.dwj.auto.comment.config.interceptor;

import com.dwj.auto.comment.common.utils.PathMatcherUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName AccessInterceptor
 * @Description 访问拦截器
 * @Author dwjian
 * @Date 2020/1/12 23:49
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix="spring.interceptor",name = "accessInterceptor", havingValue = "true")
public class AccessInterceptor extends AbstractInterceptor implements HandlerInterceptor {

    private static final Set<String> BLACK_LIST = new HashSet<>();

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        for (String matchPath : BLACK_LIST) {
            if (PathMatcherUtil.match(matchPath, uri)) {
                response.sendRedirect(request.getContextPath()+"/page/404.html");
                return false;
            }
        }
        return true;
    }

    @Override
    public String pathPatterns() {
        return "/**";
    }

    @Override
    public Set<String> excludePathPatterns() {
        return new HashSet<>();
    }

    @Override
    public int order() {
        return 0;
    }
}
